This Rmarkdown is a makes various state or google map forms. Has code to download various map types, crop, color by continuous or discrete variables, and label points. No where near comprehensive, just the basics.
library(tidyverse)
library(ggrepel)
library(ggmap)
library(ggsci)
library('wesanderson')
library(scales)
Tons of options built into ggmap to get data for various countries / states
states <- map_data('state')
states_map <- ggplot() +
geom_polygon(data=states,aes(x = long,y = lat,group = group),
fill='grey70',color = "black") +
coord_fixed(1.3) + #this adjusts lat long ratio
xlab("Longitude") + ylab("Latitude") + theme_bw() +
theme(axis.title = element_text(size=20,face = 'bold'),
axis.text = element_text(size=16),
legend.position = 'none',
legend.title = element_text(size=18,face = 'bold'),
panel.border = element_rect(size = 1.5, colour = "black"),
legend.text = element_text(size=18),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
states_map
states <- map_data('state')
select_states <- c('california', 'oregon','nevada','utah','arizona','montana',
'washington','wyoming','idaho','new mexico','colorado')
west <- states[which(states$region %in% select_states),]
Picking colors: 11 states so need 11 colours. ggsci package is nice. npg only has 10 so use that then and one more
colWest <- c(pal_npg()(10)[1:10],'grey70')
show_col(colWest)
west_map <- ggplot() +
geom_polygon(data=west,aes(x = long,y = lat,fill = region,
group = group),color = "black") +
scale_fill_manual(name='State:',values = colWest) +
coord_fixed(1.3) +
xlab("Longitude") + ylab("Latitude") + theme_bw() +
theme(axis.title = element_text(size=20,face = 'bold'),
axis.text = element_text(size=16),
legend.position = 'none',
legend.title = element_text(size=18,face = 'bold'),
panel.border = element_rect(size = 1.5, colour = "black"),
legend.text = element_text(size=18),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
west_map
Metadata: Pop = Population ID Lat = Latitude Long = Longitude State = Abbreviated State Elevation = Elevation (m)
data <- read.csv('ggmap_data.csv')
range(data$Long)
## [1] -122.0870 -106.4966
range(data$Lat)
## [1] 34.32487 46.47515
Tons and tons of options under maptype. I am only showing one.
maptype = c(“terrain”, “terrain-background”, “terrain-labels”, “terrain-lines”, “toner”, “toner-2010”, “toner-2011”, “toner-background”, “toner-hybrid”, “toner-labels”, “toner-lines”, “toner-lite”, “watercolor”)
Just one site with tutorial: https://www.r-graph-gallery.com/324-map-background-with-the-ggmap-library.html
Zoom: increasing zoom will increase resolution but might lose major details. Play with this option depending upon spatial structure of data. Lower zoom will give state lines, higher zoom can give street names.
bbox: Use the range of your data to select which range of map
map <- get_stamenmap(bbox = c(left = -123, bottom = 33, right = -106, top = 47),
zoom=5, maptype = 'terrain')
stamen_map <- ggmap(map); stamen_map
Map will have small white points at exact location with a label that is repelled from points so they don’t overlap. Making the map different sizes will change how cluttered things are. Labels will be filled by state colour.
population_map <- ggmap(map) +
geom_point(data = data, aes(x = Long, y = Lat),
size=2, colour='black',fill='white',pch=21) +
geom_label_repel(data = data, aes(x = Long, y = Lat,label = Pop,fill=State),
colour = "black", fontface = "bold",size=6) +
scale_fill_npg(name='State:') +
xlab("Longitude") + ylab("Latitude") + theme_bw() +
theme(axis.text = element_text(size=16),
axis.title = element_text(size = 20, colour="black",face = "bold"),
panel.border = element_rect(size = 1.5, colour = "black"),
legend.text = element_text(size = 13),
legend.position = 'none',
legend.title = element_text(size = 16, face = "bold"),
panel.grid = element_blank())
population_map
Same as above but coloured by Elevation. Colour chosen from ‘wesanderson’ package
colElev <- wes_palette("Zissou1", 1000, type = "continuous")
elevation_map <- ggmap(map) +
geom_point(data = data, aes(x = Long, y = Lat),
size=2, colour='black',fill='white',pch=21) +
geom_label_repel(data = data, aes(x = Long, y = Lat,label = Pop,fill=Elevation),
colour = "black", fontface = "bold",size=6) +
scale_fill_gradientn(name='Elevation:',colours=colElev) +
xlab("Longitude") + ylab("Latitude") + theme_bw() +
theme(axis.text = element_text(size=16),
axis.title = element_text(size = 20, colour="black",face = "bold"),
panel.border = element_rect(size = 1.5, colour = "black"),
legend.text = element_text(size = 13),
#legend.position = 'none',
legend.title = element_text(size = 16, face = "bold"),
panel.grid = element_blank())
elevation_map